package io.anran0.litemusic;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //界面列表控件
    ListView lv;
    //所有音乐列表
    List<Music> ls = new ArrayList<>();

    //音乐播放器选项
    private static MediaPlayer mediaPlayer = null;
    //当前在播放音乐播放器数量
    private int nowMusic = 0;
    //显示音乐的文本
    TextView tv_music;
    //界面按钮
    Button bt_next, bt_last, bt_stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //绑定界面上所有的id按钮
        findid();

        //初始化音乐列表
        initLs();
        lv.setAdapter(new ArrayAdapter<Music>(this, android.R.layout.simple_list_item_1, ls));

        //单击音乐名，播放音乐
        lv.setOnItemClickListener(onItemClickListener);
        //初始化下一首音乐按钮
        bt_next.setOnClickListener(onClickListener);
        //初始化上一首音乐按钮
        bt_last.setOnClickListener(onClickListener);
        //初始化暂停音乐按钮
        bt_stop.setOnClickListener(onClickListener);

    }

    /**
     * 按钮监听器
     */
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == bt_next) {//下一首音乐功能呢
                nowMusic = (1 + nowMusic) % ls.size();
                ls.get(nowMusic).start();
            } else if (v == bt_last) {//商业上音乐功能
                nowMusic = (nowMusic - 1 < 0 )? ls.size() - 1 : nowMusic - 1;
                ls.get(nowMusic).start();
            } else if (v == bt_stop) {//暂停播放功能
                if (mediaPlayer == null)//如果一直没用播放，不用管
                    return;
                if (mediaPlayer.isPlaying())//如果在播放，则暂停
                    mediaPlayer.pause();
                else//如果不在播放，则播放
                    mediaPlayer.start();
            }
        }
    };

    /**
     * 与界面绑定按钮
     */

    private void findid() {
        lv = findViewById(R.id.music_list);
        tv_music = findViewById(R.id.main_tv_musicname);
        bt_last = findViewById(R.id.main_bt_lastmusic);
        bt_stop = findViewById(R.id.main_bt_stopmusic);
        bt_next = findViewById(R.id.main_bt_nextmusic);
        bt_stop.setOnClickListener((v) -> {
            if (mediaPlayer.getCurrentPosition() == MediaPlayer.MEDIA_INFO_STARTED_AS_NEXT)
                mediaPlayer.start();

        });
    }

    /**
     * 为列表控件做监听器
     */
    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            nowMusic = position;
            ls.get(position).start();
        }
    };

    /**
     * 初始化
     */
    private void initLs() {
        ls.add(new Music("遥不可及-花粥", R.raw.music1));
        ls.add(new Music("往后余生-许嵩", R.raw.music2));
        ls.add(new Music("有何不可-许嵩", R.raw.music3));
        ls.add(new Music("素颜-许嵩", R.raw.music4));
        ls.add(new Music("隐形的翅膀-张韶涵", R.raw.music5));
    }

    /**
     * 音乐类
     */
    private class Music {


        public Music(String name, int rawid) {
            this.name = name;
            this.rawid = rawid;
        }

        //音乐名称
        private String name;
        private int rawid;

        @Override
        public String toString() {
            return name;
        }

        //播放当前这首歌
        public void start() {
            //更新界面上的当前音乐名称
            tv_music.setText(name);
            //如果关闭当前音乐
            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }
            //播放这首音乐
            mediaPlayer = MediaPlayer.create(MainActivity.this, rawid);
            mediaPlayer.start();
        }
    }

}